package org.darkforest.merchant.controller;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.dao.mapper.SpecMapper;
import org.darkforest.dao.mapper.SpecValueMapper;
import org.darkforest.model.Spec;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 商品多规格管理
 * Created by Administrator on 2017/3/31.
 */
@Controller
@RequestMapping(value = "spec")
public class SpecController {


    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private SpecValueMapper specValueMapper;


    @RequestMapping(value = "specList")
    public ModelAndView specList(@RequestParam(required = false, defaultValue = "1") int p,
                                 @RequestParam(required = false) String spName,
                                 @RequestParam(required = false) String classId){
        ModelAndView res = new ModelAndView("spec/spec_list");
        Spec spec = new Spec();
        spec.setStart(p);
        spec.setSpParentId(0);
        spec.setIsQueryRelation(true);

        spec.setSpName(StringUtils.isBlank(spName) ? null : spName);
        spec.setClassId(StringUtils.isBlank(classId) ? null : Integer.parseInt(classId));
        List<Spec>  specList= specMapper.findSpecWithSonByCondition(spec);
        spec.setResultList(specList);
        res.addObject("pageBean",spec);
        return res;
    }

    /**
     * 新增规格与商品分类表关联
     * @return
     */
    @RequestMapping(value = "toAddSpec")
    public String toAddSpec(){
        return "spec/spec_add";
    }

    /**
     * 规格插入
     * @param spec
     * @return
     */
    @RequestMapping(value = "doAddSpec")
    public @ResponseBody boolean doAddSpec(Spec spec){

        spec.setSpParentId(0);
        List<Spec> specList = specMapper.findSpec(new Spec(spec.getClassId()));
        if(specList.size()<1){
            int i = specMapper.insertSelective(spec);
            if(i==1)
                return true;
            else
                return false;
        }else{
            return false;
        }

    }


    @RequestMapping(value = "toManagerSpec")
    public ModelAndView toManagerSpec(Integer spId){
        ModelAndView res = new ModelAndView("spec/spec_manager");
        Spec  spec= specMapper.getSpecWithChildrenById(spId);
        res.addObject("specInfo",spec);
        return res;
    }


    /**
     * 规格插入
     * @param spec
     * @return
     */
    @RequestMapping(value = "doAddSpecBatch")
    public @ResponseBody boolean doAddSpecBatch(Spec spec){


        for(Spec record:spec.getSpecList()){
            if(StringUtils.isBlank(record.getSpName()))
                continue;
            specMapper.insertSelective(record);
        }

        return true;
    }


    @RequestMapping(value = "delSpec")
    public @ResponseBody
    AjaxReturnData  delSpec(Integer[] spIds){
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        for(Integer spId:spIds){


            Spec spec = new Spec();
            spec.setSpParentId(spId);
            List<Spec> specList = specMapper.findSpec(spec);
            for(Spec record:specList){
                //删除对应的属性值
                specValueMapper.deleteBySpId(record.getSpId());
            }

            //删除子记录
            specMapper.deleteByParentId(spId);


            //删除本记录
            specMapper.deleteByPrimaryKey(spId);

        }
        ajaxReturnData.setMessage("删除成功");
        ajaxReturnData.setType("succ");
        ajaxReturnData.setUrl("spec/specList");
        return ajaxReturnData;
    }



}
